home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_02 / boxes1.cpp < prev    next >
C/C++ Source or Header  |  1992-01-18  |  1KB  |  60 lines

  1.                                  // Chapter 5 - Program 6
  2. #include <iostream.h>
  3.  
  4. class box {
  5.    int length;
  6.    int width;
  7. public:
  8.    box(void);         //Constructor
  9.    void set(int new_length, int new_width);
  10.    int get_area(void) {return (length * width);}
  11.    ~box(void);        //Destructor
  12. };
  13.  
  14.  
  15. box::box(void)        //Constructor implementation
  16. {
  17.    length = 8;
  18.    width = 8;
  19. }
  20.  
  21.  
  22. // This method will set a box size to the two input parameters
  23. void box::set(int new_length, int new_width)
  24. {
  25.    length = new_length;
  26.    width = new_width;
  27. }
  28.  
  29.  
  30. box::~box(void)       //Destructor
  31. {
  32.    length = 0;
  33.    width = 0;
  34. }
  35.  
  36.  
  37. main()
  38. {
  39. box small, medium, large;          //Three boxes to work with
  40.  
  41.    small.set(5, 7);
  42.                           // Note that the medium box uses the values
  43.                           // supplied by the constructor
  44.    large.set(15, 20);
  45.    
  46.    cout << "The small box area is " << small.get_area() << "\n";
  47.    cout << "The medium box area is " << medium.get_area() << "\n";
  48.    cout << "The large box area is " << large.get_area() << "\n";
  49.    
  50. }
  51.  
  52.  
  53.  
  54.  
  55. // Result of execution
  56. //
  57. // The small box area is 35
  58. // The medium box area is 64
  59. // The large box area is 300
  60.